package com.greenline.push.home.controllers.classdefine;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenline.guser.annotation.GuserResource;
import com.greenline.push.common.json.BaseJson;
import com.greenline.push.common.json.GridJsonData;
import com.greenline.push.core.pushcenter.AreaManager;
import com.greenline.push.core.pushcenter.ClassBatchDefineManager;
import com.greenline.push.core.pushcenter.ClassDefineManager;
import com.greenline.push.core.pushcenter.ClassUserManager;
import com.greenline.push.dal.pushcenter.dataobject.ClassDefineDO;
import com.greenline.push.dal.query.ClassDefineQuery;

@Controller
@RequestMapping(value = "/class")
public class ClassController {

    @Autowired
    private ClassDefineManager classManager;

    @Autowired
    private ClassBatchDefineManager batchManager;

    @Autowired
    private ClassUserManager userManager;

    @Autowired
    private AreaManager areaManager;

    private final Log logger = LogFactory.getLog(ClassController.class);

    @GuserResource("用户分组列表")
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public String queryRecord(ModelMap model) {
        return "class/index";
    }

    @GuserResource("用户分组列表")
    @RequestMapping("/query.json")
    @ResponseBody
    public GridJsonData<ClassDefineDO> listRecord(ClassDefineQuery query,
            @RequestParam(value = "page", defaultValue = "1", required = false) String pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) String pageSize) {
        query.setPageIndex(Integer.parseInt(pageIndex));
        query.setPageSize(Integer.parseInt(pageSize));
        GridJsonData<ClassDefineDO> ret = new GridJsonData<ClassDefineDO>();
        ret.setQuery(classManager.listByQuery(query));
        return ret;
    }

    @GuserResource("筛选用户分组添加")
    @RequestMapping(value = "/filter_add", method = RequestMethod.GET)
    public String filterAddInit(ModelMap model) {
        model.put("provinces", areaManager.listProvinces());
        return "class/filter_add";
    }

    @GuserResource("上传用户分组添加")
    @RequestMapping(value = "/upload_add/{id}", method = RequestMethod.GET)
    public String uploadAddInit(ModelMap model, @PathVariable Long id) {
        ClassDefineDO object = classManager.getClassDefineById(id);
        model.put("classdefine", object);
        return "class/upload_add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addInit(ModelMap model) {
        return "class/add";
    }

    @GuserResource("用户分组添加")
    @RequestMapping(value = "/add.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson add(ModelMap model, @ModelAttribute("classDefine") ClassDefineDO classDO) {
        BaseJson json = new BaseJson();

        try {
            ClassDefineDO s = classManager.getByCode(classDO.getCode());
            boolean result = false;
            if (s == null) {
                result = classManager.insert(classDO) > 0;
            }
            if (result) {
                json.setMessage("提交成功！");
            } else {
                json.setHasError(true);
                json.setMessage("提交出错,请检查组编码是否重复");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("提交出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }

    @GuserResource("用户分组修改")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String updateInit(ModelMap model, @PathVariable Long id) {
        ClassDefineDO object = classManager.getClassDefineById(id);
        model.put("classdefine", object);
        return "class/update";
    }

    @GuserResource("用户分组修改")
    @RequestMapping(value = "/update.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson update(ModelMap model, @ModelAttribute("classdefine") ClassDefineDO classDO) {
        BaseJson json = new BaseJson();
        try {
            if (classManager.update(classDO)) {
                json.setMessage("提交成功！");
            } else {
                json.setHasError(true);
                json.setMessage("提交出错");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("提交出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }

    @GuserResource("用户分组删除")
    @RequestMapping(value = "/delete.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson delete(ModelMap model, @ModelAttribute("id") Long id) {
        BaseJson json = new BaseJson();
        try {
            if (classManager.delete(id)) {
                json.setMessage("删除成功！");
            } else {
                json.setHasError(true);
                json.setMessage("删除出错");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("删除出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }

    @GuserResource("用户分组列表")
    @RequestMapping(value = "/list-allclass", method = RequestMethod.GET)
    public String listallClass(ModelMap model) {
        return "class/listall-class";
    }
}
